*Create "self-indulgence index" (higher value means less self-control)
gen self_indulgence = (alc + gambling_exp + gaming + lottery_exp  ) / month_total_income_mean
gen self_indulgence2 = (alc + gambling_exp + gaming + lottery_exp  ) / mean_total_exp
bysort ind_id: egen self_indulgence_ind = mean(self_indulgence)
bysort ind_id: egen self_indulgence_ind2 = mean(self_indulgence2)


foreach y in self_indulgence_ind self_indulgence_ind2 {

	if "`y'" == "self_indulgence_ind" {
	local format "%9.3fc"
	}
	else if "`y'" == "self_indulgence_ind2" {
	local format "%9.2fc"
	}
	
	local category "coholder frequent_coholder intense_coholder costly_coholder alc_pers gaming_pers gambling_pers tlottery_pers alwaysin2memberhh female"
	gen subcategory = ""
	gen mean = .
	gen lqt = .
	gen uqt = .
	gen n = _n
	local i = 1

	foreach var in `category' {
		if "`var'" == "coholder" | "`var'" == "frequent_coholder" | "`var'" == "intense_coholder" | "`var'" == "costly_coholder" | "`var'"== "alc_pers" | "`var'"== "gaming_pers" | "`var'"== "gambling_pers" | "`var'"== "tlottery_pers" | "`var'"== "alwaysin2memberhh" | "`var'"== "female" {
			local min 0
			local max 1
		} 
		 forvalues j = `min'/`max' { 
			replace subcategory = "`var'`j'" if n == `i'    
			ci means `y' if `var' == `j' & edate_id==1335, level(90)
			gen m = `r(mean)'
			gen l = `r(lb)'
			gen u = `r(ub)'
			replace mean = m if subcategory == "`var'`j'"
			replace lqt  = l if subcategory == "`var'`j'"
			replace uqt  = u if subcategory == "`var'`j'"
			local i = `i' + 1
			drop m l u
		 }
		local i = `i' + 1	 
	}

	sort n
	egen min = min(lqt*10)
	egen max = max(uqt*10)

	twoway ///
	rcap uqt lqt n if subcategory != "", lstyle(ci) ||  ///
	 scatter mean n if subcategory != "", ///
	 mcolor(blue) mstyle(p1) ///
	 title("Degree of self-indulgence", place(left) size(large)) ///
	 ytitle("") ///
	 xtitle("") ///
	 xline(3 6 9 12 15 18 21 24 27, lpattern(dot)) ///
	 ylabel(, labsize(medium) angle(0) format("`format'")) /// 
	 xlabel(1 "No coholder" 2 "Coholder" ///
		 4 "Infrequent coholder" 5 "Frequent coholder" ///
		 7  "Light coholder" 8 "Intense coholder" ///
		 10  "Low-cost coholder" 11 "Costly coholder" ///
		 13 "No alcohol type" 14 "Alcohol type" ///
		 16 "No gaming type" 17 "Gaming type" ///
		 19 "No gambler" 20 "Gambler" ///
		 22 "No lottery type" 23 "Lottery type" ///
		 25 "Not linked" 26 "Linked" ///
		 28 "Male" 29 "Female", ///
		 labsize(medium) angle(90))  ///
	 xscale(range(0.25 29.75)) ///
	 legend(off)
	 
drop subcategory mean lqt uqt n 
drop min max  
graph export "Figures/CohExpl_SelfControl_index_`y'.png", replace
graph export "Figures/CohExpl_SelfControl_index_`y'.pdf", replace
	 
}

foreach y in self_indulgence self_indulgence2 {

	if "`y'" == "self_indulgence" {
	local format "%9.4fc"
	}
	else if "`y'" == "self_indulgence2" {
	local format "%9.2fc"
	}

	local category "cohold_day cohold_day_frequent cohold_day_intense cohold_day_costly cohold_spell_cat"
	gen subcategory = ""
	gen mean = .
	gen lqt = .
	gen uqt = .
	gen n = _n
	local i = 1

	foreach var in `category' {
		if "`var'" == "cohold_day" | "`var'" == "cohold_day_frequent" | "`var'" == "cohold_day_intense" | "`var'" == "cohold_day_costly" {
			local min 0
			local max 1
		} 
		else if "`var'" == "cohold_spell_cat" {
			local min 0
			local max 5
		} 
		
		 forvalues j = `min'/`max' { 
			replace subcategory = "`var'`j'" if n == `i'    
			ci means `y' if `var' == `j' & coholder==1, level(90)
			gen m = `r(mean)'
			gen l = `r(lb)'
			gen u = `r(ub)'
			replace mean = m if subcategory == "`var'`j'"
			replace lqt  = l if subcategory == "`var'`j'"
			replace uqt  = u if subcategory == "`var'`j'"
			local i = `i' + 1
			drop m l u
		 }
		local i = `i' + 1	 
	}

	sort n
	egen min = min(lqt*10)
	egen max = max(uqt*10)

	twoway ///
	rcap uqt lqt n if subcategory != "", lstyle(ci) ||  ///
	scatter mean n if subcategory != "", ///
	mcolor(blue) mstyle(p1) ///
	title("Degree of self-indulgence", place(left) size(large)) ///
	ytitle("") ///
	xtitle("") ///
	xline(3 6 9 12, lpattern(dot)) ///
	ylabel(, labsize(medium) angle(0) format("`format'")) /// 
	xlabel(1 `""Coholder:" "No cohold day""' 2 "Cohold day" ///
		 4 `""Frequent coholder:" "No cohold day""' 5 "Cohold day" ///
		 7  `""Intense coholder:" "No cohold day""' 8 "Cohold day" ///
		 10  `""Costly coholder:" "No cohold day""' 11 "Cohold day" ///
		 13  `""Coholder:" "No cohold day""' 14 "Start" 15 "First 1/3" 16 "Intermediate 1/3" 17 "Last 1/3" 18 "End", ///
		 labsize(medium) angle(90))  ///
	 xscale(range(0.25 18.75)) ///
	 legend(off) 
	 
drop subcategory mean lqt uqt n 
drop min max  
graph export "Figures/CohExpl_SelfControl_index2_`y'.pdf", replace
graph export "Figures/CohExpl_SelfControl_index2_`y'.png", replace
}

*Mean of indices within cohold spell category
foreach var in self_indulgence self_indulgence2 {
bysort cohold_spell_cat_id: egen `var'_spell = mean(`var') 
}
foreach var in ///
	self_indulgence self_indulgence2 {
bysort cohold_spell_cat: egen mean = mean(`var'_spell) if coholder==1 & cohold_spell_cat_uoa == 1
bysort cohold_spell_cat: egen std = sd(`var'_spell) if coholder==1 & cohold_spell_cat_uoa == 1
bysort cohold_spell_cat: egen obs = count(dum) if coholder==1 & cohold_spell_cat_uoa == 1
gen se = std/sqrt(obs) if coholder==1 & cohold_spell_cat_uoa == 1
bysort cohold_spell_cat: egen n = seq() if coholder==1 & cohold_spell_cat_uoa == 1

serrbar mean se cohold_spell_cat if n ==1, /// 
mvopts(mcolor(blue)) ///
scale(1.64) ///
title("Degree of self-indulgence", place(left) size(large)) ///
ytitle("") ///
xtitle("Point in coholding cycle", size(large)) ///
ylabel(, labsize(medium) angle(0) format(%9.4fc)) ///
xlabel(0 "No cohold day" 1 "Start" 2 "First 1/3" 3 "Intermediate 1/3" 4 "Last 1/3" 5 "End", ///
	 labsize(medium)) ///
xscale(range(-0.25 5.25)) 

drop mean std obs se n
graph export "Figures/CohExpl_SelfControl_index3_`var'.pdf", replace
graph export "Figures/CohExpl_SelfControl_index3_`var'.png", replace

}

*T-test: 
eststo a: quietly estpost summarize self_indulgence2 alc_exp gambling_exp_exp gaming_exp lottery_exp_exp if coholder==1 & cohold_day==0, detail
eststo b: quietly estpost summarize self_indulgence2 alc_exp gambling_exp_exp gaming_exp lottery_exp_exp if coholder==1 & cohold_day==1, detail
eststo diff: quietly estpost ttest self_indulgence2 alc_exp gambling_exp_exp gaming_exp lottery_exp_exp if coholder==1 & cohold_day==1| coholder==1 & cohold_day==0, by(cohold_day)

esttab a b diff using "Tables/Coh_expl_SelfControl_ttest.tex", replace ///
cells("mean(label(Mean) pattern(1 1 0) fmt(%15.2fc)) p25(label(P25) pattern(1 1 0) fmt(%15.2fc)) p50(label(P50) pattern(1 1 0) fmt(%15.2fc)) p75(label(P75) pattern(1 1 0) fmt(%15.2fc)) p90(label(P90) pattern(1 1 0) fmt(%15.2fc)) b(label(Diff.) star pattern(0 0 1) fmt(%15.2fc)) se(label(SE) par pattern(0 0 1) fmt(%15.2fc))") ///
mtitle("\shortstack{Coholder: \\ No cohold day}" "\shortstack{Coholder:\\ Cohold day}" "") ///
booktabs noobs nonumbers ///
starlevels(* 0.10 ** 0.05 *** 0.01) ///
rename( ///
self_indulgence2 "Degree of self-indulgence" ///
alc_exp "Alcohol" gambling_exp_exp "Gambling expenditures" ///
gaming_exp "Gaming" lottery_exp_exp "Lottery expenditures") ///
substitute(? \) 



